<?php
/**
 * @title 1030. 距离顺序排列矩阵单元格
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }



class Solution {

    /**
     * @param Integer $R
     * @param Integer $C
     * @param Integer $r0
     * @param Integer $c0
     * @return Integer[][]
     */
    function allCellsDistOrder($R, $C, $r0, $c0) {
        /**
         * @since 2020-11-25 初始化
         */
        $grid = array_fill(0, $R, array_fill(0, $C, 0));

        /**
         * @since 2020-11-25 结果集
         */
        $result = [[$r0, $c0]];

        /**
         * @since 2020-11-25 迭代
         */
        $grid[$r0][$c0] = 1;
        $nexts = [[$r0, $c0]];
        while($nexts){
            $nextTmp = [];

            /**
             * @since 2020-11-25 迭代每个点
             */
            foreach ($nexts as list($r, $c)){
                /**
                 * @since 2020-11-25 向上
                 */
                if($r>=1 AND $grid[$r-1][$c] == 0){
                    $grid[$r-1][$c] = 1;
                    $nextTmp[] = [$r-1, $c];
                    $result[] = [$r-1, $c];
                } else {}

                /**
                 * @since 2020-11-25 向下
                 */
                if($r<=$R-2 AND $grid[$r+1][$c] == 0){
                    $grid[$r+1][$c] = 1;
                    $nextTmp[] = [$r+1, $c];
                    $result[] = [$r+1, $c];
                }

                /**
                 * @since 2020-11-25 向左
                 */
                if($c>=1 AND $grid[$r][$c-1] == 0){
                    $grid[$r][$c-1] = 1;
                    $nextTmp[] = [$r, $c-1];
                    $result[] = [$r, $c-1];
                }

                /**
                 * @since 2020-11-25 向右
                 */
                if($c<=$C-2 AND $grid[$r][$c+1] == 0){
                    $grid[$r][$c+1] = 1;
                    $nextTmp[] = [$r, $c+1];
                    $result[] = [$r, $c+1];
                }
            }

            /**
             * @since 2020-11-25 继续迭代
             */
            $nexts = $nextTmp;
        }

        /**
         * @return
         */
        return $result;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202011/2020.11.17.php
 */
$datas = [
    [1,2,0,0],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->allCellsDistOrder(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
